package com.lin.services.impl;

import com.lin.dao.CategoryDao;
import com.lin.dao.StockDao;
import com.lin.dao.WaresDao;
import com.lin.pojo.Category;
import com.lin.pojo.Wares;
import com.lin.services.StockReportService;
import com.lin.utils.ShiroUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName:StockServiceImpl
 * Package:com.lin.services.impl
 * Author: hellcat
 * Date: 2020/1/7
 * ♣♣♣
 */

@Service
public class StockReportServiceImpl implements StockReportService {
    @Autowired
    private StockDao stockDao;
    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private WaresDao waresDao;

    @Override
    public List<Wares> getStock(String idOrName, String category1, String category2) {
        return stockDao.getStock(idOrName, category1, category2, ShiroUtil.getUser().getStoreId());
    }

    @Override
    public Map<String, Object> updateMinInventory(String SId, String SMinInventoryValue) {
        Map<String, Object> map = new HashMap<>();
        try {
            Integer id = Integer.parseInt(SId);
            Integer minInventoryValue = Integer.parseInt(SMinInventoryValue);
            Integer i = stockDao.updateMinInventory(id, minInventoryValue, ShiroUtil.getUser().getStoreId());
            if (i == 1) {
                map.put("code", true);
            }
        } catch (Exception e) {
            map.put("code", false);
            map.put("msg", "修改失败，非法数据");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return map;
    }

    @Override
    public Map<String, Object> updateInventory(String id, String inventoryValue) {
        Map<String, Object> map = new HashMap<>();
        try {
            Integer idI = Integer.parseInt(id);
            Integer inventoryValueI = Integer.parseInt(inventoryValue);
            //此次修改库存，属于非法修改，没有直接数据影响进价，暂不做新进价修改
            int i = stockDao.updateInventory(idI, inventoryValueI, ShiroUtil.getUser().getStoreId());
            if (i == 1) {
                map.put("code", true);
            }
        } catch (Exception e) {
            map.put("code", false);
            map.put("msg", "修改失败，非法数据");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return map;
    }

    @Override
    public Map<String, Object> loadStockReport() {
        Map<String, Object> map = new HashMap<>();
        try {
            int storeId = ShiroUtil.getUser().getStoreId();
            List<Category> categories = categoryDao.listCategory1(storeId);
            List<Map<String, Object>> dataList = new ArrayList<>();
            for (Category category : categories) {
                double Purchase = stockDao.getPurchaseByCategoryId(category.getCategoryId(),
                        ShiroUtil.getUser().getStoreId());
                Map<String, Object> dataMap = new HashMap<>();
                dataMap.put("value", Purchase);
                dataMap.put("name", category.getCategoryName());
                dataList.add(dataMap);
            }
            map.put("data", dataList);
            map.put("code", true);
        } catch (Exception e) {
            map.clear();
            map.put("code", false);
            map.put("msg", "数据加载失败");
        }
        return map;
    }

    @Override
    public Map<String, Object> showStockData() {
        Map<String, Object> map = new HashMap<>();
        int storeId = ShiroUtil.getUser().getStoreId();
//        负库存计入商品数，不计入商品价值计算
        try {
            int waresTotals = waresDao.getWaresTotalsByStoreId(storeId);
            double castTotals = waresDao.getCastTotalsByStoreId(storeId);
            double priceTotals = waresDao.getPriceTotalsByStoreId(storeId);
            map.put("code", true);
            map.put("waresTotals", waresTotals);
            map.put("castTotals", castTotals);
            map.put("priceTotals", priceTotals);
        } catch (Exception e) {
            map.clear();
            map.put("code", false);
            map.put("msg", "获取失败");
        }
        return map;
    }
}
